package cn.com.blueInfo.bpm.login.service.impl;

import cn.com.blueInfo.bpm.login.request.LoginInfoDTO;
import cn.com.blueInfo.bpm.login.service.LoginUserService;
import cn.com.blueInfo.bpm.person.request.UserAccountDTO;
import cn.com.blueInfo.bpm.person.response.UserAccountVO;
import cn.com.blueInfo.bpm.person.service.UserAccountService;
import cn.com.blueInfo.core.util.JwtUtils;
import cn.com.blueInfo.utils.error.BusinessException;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 用户基本信息服务实现
 * @author suxch
 * @date 2018年12月19日  下午1:33:57
 */
@Log4j2
@Service
public class LoginUserServiceImpl implements LoginUserService {

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResultInfo<Map<String, Object>> loginInfo(LoginInfoDTO loginInfoDTO) {
        Map<String, Object> claims = new HashMap<>();
        // 这里面后续需要加入用户的一些关键信息。便于后续处理

        String loginName = loginInfoDTO.getLoginName();
        String password = loginInfoDTO.getPassword();

        UserAccountDTO.QueryDTO userAccountQueryDTO = new UserAccountDTO.QueryDTO();
        userAccountQueryDTO.setLoginName(loginName);

        ResultInfo<Page<UserAccountVO>> resultUserAccount = userAccountService.queryUserAccountPage(userAccountQueryDTO);
        Page<UserAccountVO> userAccountVOPage = resultUserAccount.getData();
        List<UserAccountVO> userAccountVOList = userAccountVOPage.getRecords();
        UserAccountVO userAccountVO = userAccountVOList.get(0);
        String passwordCipher = userAccountVO.getPassword();
        boolean isLogin = passwordEncoder.matches(password, passwordCipher);

        if (isLogin) {

            claims.put("tenantId", userAccountVO.getTenantId());
            claims.put("applicationId", userAccountVO.getApplicationId());
            claims.put("loginName", userAccountVO.getLoginName());
            claims.put("userId", userAccountVO.getId());

            String accessToken = jwtUtils.createToken(loginName, claims);
            String refreshToken = generateRefreshToken(); // 实现RefreshToken生成逻辑

            // 7. 返回Token响应
            Map<String, Object> result = new HashMap<>();
            result.put("access_token", accessToken);
            result.put("refresh_token", refreshToken);
            result.put("token_type", "Bearer");
            result.put("expires_in", jwtUtils.getExpiration() / 1000); // 转换为秒
            return ResultInfo.<Map<String, Object>>builder().data(result).build();
        } else {
            throw new BusinessException(500, "密码输入错误，登录失败");
        }
    }

    private String generateRefreshToken() {
        // 实现RefreshToken生成逻辑
        return UUID.randomUUID().toString().replace("-", "");
    }

}

